package com.grgbanking.it.project.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.grgbanking.it.base.BaseDao;
import com.grgbanking.it.project.dao.IOperationWorkHandleDao;
import com.grgbanking.it.project.entity.OperationWorkHandle;


@Repository 
public class OperationWorkHandleDao extends BaseDao<OperationWorkHandle, Integer> implements IOperationWorkHandleDao<OperationWorkHandle, Integer> {
	/** 
     * 给HibernateDaoSupport注入一个SessionFactory. 
     * 如果是xml的话，是直接把sesionFactory注入就行了。而这里需要一个额外的setter方法。 
     */  
    @Autowired  
    public void setMySessionFactory(SessionFactory sessionFactory){  
        super.setSessionFactory(sessionFactory);  
    }

    public OperationWorkHandle findToHandle(Integer workId,String username,Integer...state){
    	StringBuffer hql = new StringBuffer("from OperationWorkHandle t\n");
    	hql.append("where t.username='").append(username).append("' and t.operationWork.id=").append(workId);
    	if(state != null){
    		hql.append(" and (1=2");
        	for(Integer s : state){
        		hql.append(" or t.state=").append(s);
        	}
        	hql.append(")");
    	}
    	
    	System.out.println(hql);
    	return this.findOneByHQL(hql.toString());
    }
    
    public List<OperationWorkHandle> findByWorkId(Integer workId){
    	StringBuffer hql = new StringBuffer("from OperationWorkHandle t\n");
    	hql.append("where t.operationWork.id=").append(workId);
    	
    	return this.findByHql(hql.toString());
    }
}